package com.tliaswebmanagement.controller;

import com.github.pagehelper.PageHelper;
import com.tliaswebmanagement.pojo.Emp;
import com.tliaswebmanagement.pojo.Result;
import com.tliaswebmanagement.pojo.pageBean;
import com.tliaswebmanagement.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 根据id批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteIds(@PathVariable List<Integer> ids) {
        empService.deleteIds(ids);
        return Result.success();
    }

    /**
     * 分页查询 / 模糊查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @GetMapping
    public Result pageQuery(
            @RequestParam(defaultValue = "1", required = false) Integer page,
            @RequestParam(defaultValue = "10", required = false) Integer pageSize,
            String name, Short gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {

        pageBean pb = empService.pageQuery(page, pageSize, name, gender, begin, end);
        return Result.success(pb);
    }

    /**
     * 添加员工
     *
     * @param emp
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Emp emp) {
        log.info("添加的员工={}", emp);
        empService.add(emp);
        return Result.success();
    }

    /**
     * 根数据修改 根据id回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getId(@PathVariable Integer id) {
        log.info("查询的id是:{}", id);
        Emp emp = empService.getId(id);
        return Result.success(emp);
    }

    /**
     * 数据修改
     * @param emp
     * @return
     */
    @PutMapping()
    public Result update(@RequestBody Emp emp) {
        log.info("修改的员工信息是:{}", emp);
        empService.update(emp);
        return Result.success();
    }
}
